package com.ocom.devicehttp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.request.payhandle.InvitedSmsRequest;
import com.ocom.common.request.payhandle.RegisterPersonRequest;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.*;
import com.ocom.common.vo.pay.FaceErrVo;
import com.ocom.devicehttp.feign.machine.*;
import com.ocom.devicehttp.service.MachineService;
import com.ocom.redis.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MachineServiceImpl implements MachineService {

    private Logger log = LoggerFactory.getLogger(MachineServiceImpl.class);


    @Value("${apiStatus:0}")
    private Integer apiStatus;

    
    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private MachineFeignService machineFeignService;

    @Autowired
    private PayoperFeignService payoperFeignService;

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private SrvpAccessFeignService srvpAccessFeignService;

//    @Autowired
//    MqSimpleClientService mqSimpleClientService;

    @Autowired
    XfRecodeClientService xfRecodeClientService;
//    @Autowired
//    private EmqxFeignService emqxFeignService;




    /*
     * @Description: 签到
     * @Author: lu
     * @Date: 2022/3/24 9:41
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse sign(Map<String, Object> map) {
        log.info("签到" + map);
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            RespMachMessage respMachMessage=machineFeignService.getDeviceState(machno);
            Map<String,Object> resMap=new HashMap<>();
            if (respMachMessage.getCode().equals("0000")){
                JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(respMachMessage.getData()));
                //获取单位编号
                Integer companyId=jsonObject.getInteger("companyId");
                if (companyId==null){
                    return JsonResponse.error(StateCode.companyIdCod,StateCode.companyIdMsg);
                }
                redisUtil.hmSet("HttpXfDevice:"+machno,"companyId",companyId);
                //生成sessionID存入redis
                String sessionID= RandomUtil.getRandomString(10);
                redisUtil.hmSet("HttpXfDevice:"+machno,"sessionID",sessionID);
                resMap.put("sessionID",sessionID);
                //SaaS平台编号
                Integer saasNo=jsonObject.getInteger("saasNo");
                redisUtil.hmSet("HttpXfDevice:"+machno,"saasNo",saasNo);

                redisUtil.expire("HttpXfDevice:"+machno,86400l*60l);

                //平台编号为1按照本平台走
                if (saasNo==1){
                    String versionCode=(String) map.get("versionCode");
                    RespMachMessage resGetDeviceFlag=machineFeignService.getDeviceFlag(machno,versionCode);
                    if (resGetDeviceFlag.getCode().equals("0000")){
                        JSONObject jsonGetDeviceFlag= JSON.parseObject(JSON.toJSONString(resGetDeviceFlag.getData()));
                        resMap.put("firmwareUpdate",jsonGetDeviceFlag.getInteger("firmwareUpdate"));
                        resMap.put("payFlag",1);
                        resMap.put("merchName","");
                        resMap.put("appVer","");
                        try {
                            Result result = payoperFeignService.GetMerchInfoByMachNo(companyId.longValue(),machno);
                            log.info("签到-获取商户名称- 【{}】" , result);
                            if(result.getCode()==200){
                                JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(result.getData()));
                                resMap.put("merchName",jsonObject1.getString("merchName"));
                            }
                        }catch (Exception e){
                            Result result3 = payConfigClientService.getDeviceInfoMigrate(companyId.longValue(),machno);
                            if(result3.getCode()==200){
                                redisUtil.hmSet("HttpXfDevice:"+machno,"saasNo",10);
                                resMap.put("apiStatus",apiStatus);
                                List<String> apiList =new ArrayList<>();
                                if(apiStatus == 1 ){
                                    apiList.add("https://api.ocom30.com/xf/");
                                    apiList.add("https://mj.ocomtraffic.com/xf/");
                                }else{
                                    apiList.add("https://mj.ocomtraffic.com/xf/");
                                    apiList.add("https://api.ocom30.com/xf/");
                                }
                                apiList.add("http://192.168.11.198:9322/");

                                resMap.put("apiList",apiList);
                            }else if(result3.getCode()==6003){
                                redisUtil.hmSet("HttpXfDevice:"+machno,"saasNo",10);
                            }
                        }

                        resMap.put("dateTime",System.currentTimeMillis());

                        if (jsonGetDeviceFlag.getInteger("firmwareUpdate")==1){
                            resMap.put("appVer",jsonGetDeviceFlag.getString("appVer"));
                            return JsonResponse.success(jsonGetDeviceFlag.getString("apkUrl"),resMap);
                        }else {
                            return JsonResponse.success(resMap);
                        }
                    }else {

                        Result result = payConfigClientService.getDeviceInfoMigrate(companyId.longValue(),machno);

                        if(result.getCode()==200){
                            redisUtil.hmSet("HttpXfDevice:"+machno,"saasNo",10);
                            resMap.put("apiStatus",apiStatus);
                            List<String> apiList =new ArrayList<>();

                            if(apiStatus == 1 ){
                                apiList.add("https://api.ocom30.com/xf/");
                                apiList.add("https://mj.ocomtraffic.com/xf/");
                            }else{
                                apiList.add("https://mj.ocomtraffic.com/xf/");
                                apiList.add("https://api.ocom30.com/xf/");
                            }
                            apiList.add("http://192.168.11.198:9322/");

                            resMap.put("apiList",apiList);
                            resMap.put("dateTime",System.currentTimeMillis());
                            return JsonResponse.success(resMap);
                        }

                        return JsonResponse.error(respMachMessage.getCode(),respMachMessage.getMsg());
                    }
                }else {
                    return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
                }
            }else {
                return JsonResponse.error(respMachMessage.getCode(),respMachMessage.getMsg());
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求方式:【{}】,请求参数:【{}】,返回参数:【{}】",machno,"消费签到",map,e.getMessage());
            return JsonResponse.error();
        }
    }

    /*
     * @Description:心跳
     * @Author: lu
     * @Date: 2022/3/24 16:36
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse Heartbeat(Map<String, Object> map) {
//        log.info("心跳参数 【{}】",map);
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.valueOf(map.get("companyID").toString());

            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                String versionCode=(String) map.get("versionCode");
                //从srvpaccess查询更新标识
                RespMachMessage srvpaccessGetDeviceFlag=payoperFeignService.getDeviceFlag(companyID,machno,versionCode);
                Map<String,Object> resMap=new HashMap<>();
                if (srvpaccessGetDeviceFlag.getCode().equals("0000")){

                    JSONObject jsonSrvpaccess= JSON.parseObject(JSON.toJSONString(srvpaccessGetDeviceFlag.getData()));
                    resMap.put("paramUpdate",jsonSrvpaccess.getInteger("paramUpdate"));
                    resMap.put("fkUpdate",jsonSrvpaccess.getInteger("fkUpdate"));
                    resMap.put("meal",jsonSrvpaccess.getInteger("meal"));
                    resMap.put("zhxySecret",jsonSrvpaccess.getInteger("zhxySecret"));
                    resMap.put("listUpdate",jsonSrvpaccess.getInteger("listUpdate"));

                    String ob = (String) redisUtil.hmGet("HttpXfDevice:"+machno,"version");
                    if(ob==null || !ob.equals(jsonSrvpaccess.getString("version"))){
                        redisUtil.hmSet("HttpXfDevice:"+machno,"version",jsonSrvpaccess.getString("version"));
                        redisUtil.expire("HttpXfDevice:"+machno,86400l*60l);
                    }
                    resMap.put("menuUpdate",jsonSrvpaccess.getInteger("menu"));
                    resMap.put("faceFlag",0);

                    Object flagO = redisUtil.get("faceFalg:"+machno);
                    if(flagO!=null){
                        resMap.put("faceFlag", flagO);
                        redisUtil.del("faceFalg:"+machno);
                    }
                    RespMachMessage resGetDeviceFlag=machineFeignService.getDeviceFlag(machno,versionCode);
                    if (resGetDeviceFlag.getCode().equals("0000")){
                        JSONObject jsonGetDeviceFlag= JSON.parseObject(JSON.toJSONString(resGetDeviceFlag.getData()));
                        resMap.put("firmwareUpdate",jsonGetDeviceFlag.getInteger("firmwareUpdate"));
                        resMap.put("payFlag",1);
                        if (jsonGetDeviceFlag.getInteger("firmwareUpdate")==1){
                            return JsonResponse.success(jsonGetDeviceFlag.getString("apkUrl"),resMap);
                        }else {
                            return JsonResponse.success(resMap);
                        }
                    }else {
                        log.info("心跳返回- resGetDeviceFlag 【{}】",resGetDeviceFlag);
                        return JsonResponse.error(resGetDeviceFlag.getCode(),resGetDeviceFlag.getMsg());
                    }
                }else {
                    log.info("心跳返回- srvpaccessGetDeviceFlag 【{}】",srvpaccessGetDeviceFlag);
                    return JsonResponse.error(srvpaccessGetDeviceFlag.getCode(),srvpaccessGetDeviceFlag.getMsg());
                }
            } else if (saasNo == 10){
                String versionCode = (String) map.get("versionCode");
                //从srvpaccess查询更新标识

                Map<String, Object> resMap = new HashMap<>();


                resMap.put("paramUpdate", 0);
                resMap.put("fkUpdate", 0);
                resMap.put("meal", 0);
                resMap.put("zhxySecret", 0);
                resMap.put("listUpdate", 0);
                resMap.put("menuUpdate", 0);
                resMap.put("faceFlag", 0);

                Object flagO = redisUtil.get("faceFalg:" + machno);
                if (flagO != null) {
                    resMap.put("faceFlag", flagO);
                    redisUtil.del("faceFalg:" + machno);
                }
                RespMachMessage resGetDeviceFlag = machineFeignService.getDeviceFlag(machno, versionCode);
                if (resGetDeviceFlag.getCode().equals("0000")) {
                    JSONObject jsonGetDeviceFlag = JSON.parseObject(JSON.toJSONString(resGetDeviceFlag.getData()));
                    resMap.put("firmwareUpdate", jsonGetDeviceFlag.getInteger("firmwareUpdate"));
                    resMap.put("payFlag", 1);
                    if (jsonGetDeviceFlag.getInteger("firmwareUpdate") == 1) {
                        return JsonResponse.success(jsonGetDeviceFlag.getString("apkUrl"), resMap);
                    } else {
                        return JsonResponse.success(resMap);
                    }
                } else {
                    log.info("心跳返回- resGetDeviceFlag 【{}】", resGetDeviceFlag);
                    return JsonResponse.error(resGetDeviceFlag.getCode(), resGetDeviceFlag.getMsg());
                }

            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    /*
     * @Description:设备参数
     * @Author: lu
     * @Date: 2022/3/24 17:17
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse MachinePara(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                Map<String,Object> getMachPara=new HashMap<>();
                getMachPara.put("companyId",companyID);
                getMachPara.put("machNo",machno);
                //从srvpaccess查询更新标识
                Result srvpaccessGetDeviceFlag=payoperFeignService.getMachParaTwo(getMachPara);
                if (srvpaccessGetDeviceFlag.getCode()==200){
                    JSONObject jsonGetDeviceFlag= JSON.parseObject(JSON.toJSONString(srvpaccessGetDeviceFlag.getData()));
                    Map<String,Object> reMachPara=new HashMap<>();
                    reMachPara.put("jobId",jsonGetDeviceFlag.get("jobId"));
                    reMachPara.put("heart_data",jsonGetDeviceFlag.get("heartData"));
                    reMachPara.put("machine_data",jsonGetDeviceFlag.get("machineData"));
                    //reMachPara.put("mealPara",jsonGetDeviceFlag.get("mealPara"));
                    return JsonResponse.success(reMachPara);
                }else {
                    return JsonResponse.error(String.valueOf(srvpaccessGetDeviceFlag.getCode()),srvpaccessGetDeviceFlag.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e.getMessage());
            return JsonResponse.error();
        }
    }

    /*
     * @Description:设备参数更新通知结果
     * @Author: lu
     * @Date: 2022/3/30 15:23
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse machineParaResult(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                Map<String,Object> getMachPara=new HashMap<>();
                getMachPara.put("companyId",companyID);
                getMachPara.put("machNo",machno);
                getMachPara.put("jobId",String.valueOf(map.get("jobId")));
                Result srvpaccessGetDeviceFlag=payoperFeignService.changeMachParaState(getMachPara);
                if (srvpaccessGetDeviceFlag.getCode()==200){
                    return JsonResponse.success(srvpaccessGetDeviceFlag.getData());
                }else {
                    return JsonResponse.error(String.valueOf(srvpaccessGetDeviceFlag.getCode()),srvpaccessGetDeviceFlag.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e.getMessage());
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse whiteList(Map<String, Object> map) {
        RespMachMessage changeWhiteListState=payoperFeignService.changeWhiteListState(map);
        return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
    }

    @Override
    public JsonResponse whiteListResult(Map<String, Object> map) {
        RespMachMessage changeWhiteListState=payoperFeignService.getXFWhiteListResult(map);
        return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
    }

    @Override
    public JsonResponse decodeKey(Map<String, Object> map) {
        try {
            Long companyId= Long.parseLong(String.valueOf(map.get("companyID")));
            String machNo= (String) map.get("machNo");
            RespMachMessage changeWhiteListState=srvpAccessFeignService.getDecodeKey(companyId,machNo,2);
            return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
        }catch (Exception e){
            log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】","获取解码密钥",map.get("machNo"),map,e);
            return JsonResponse.error(StateCode.RequiredNullCod,StateCode.RequiredNullMsg);
        }
    }

    /*
     * @Description:离线码解码密钥更新结果
     * @Author: lu
     * @Date: 2022/7/8 15:58
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse decodeKeyResult(Map<String, Object> map) {
        try {
            Long companyId= Long.parseLong(String.valueOf(map.get("companyID")));
            String machNo= (String) map.get("machNo");
            String jobId= (String) map.get("jobId");
            RespMachMessage changeWhiteListState=srvpAccessFeignService.changeKeyState(companyId,machNo,jobId,0);
            return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
        }catch (Exception e){
            log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】","离线码解码密钥更新结果",map.get("machNo"),map,e);
            return JsonResponse.error(StateCode.RequiredNullCod,StateCode.RequiredNullMsg);
        }
    }

    /*
     * @Description:FkConfig
     * @Author: lu
     * @Date: 2022/7/15 15:20
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse fkConfig(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                RespMachMessage changeWhiteListState=payoperFeignService.getMachFk(companyID,machno);
                return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e.getMessage());
            return JsonResponse.error();
        }
    }

    /*
     * @Description:风控参数更新结果
     * @Author: lu
     * @Date: 2022/7/15 15:39
     * @param map:
     * @return: com.ocom.common.utils.JsonResponse
     **/
    @Override
    public JsonResponse fkConfigResult(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                String jobId= (String) map.get("jobId");
                RespMachMessage changeWhiteListState=payoperFeignService.changeFkState(companyID,machno,jobId);
                return JsonResponse.success(changeWhiteListState.getCode(),changeWhiteListState.getMsg(),changeWhiteListState.getData());
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e.getMessage());
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse faceErr(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号

            Integer companyID= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"companyId");

            Integer loginNum = Integer.valueOf(map.get("loginNum").toString());
            String[]  ids =  ((String) map.get("loginFailedInf")).split(",");

            ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));

            List<Long> idsLong =new ArrayList<>();

            try {
                String date =DateUtils.getStrDate(new Date(),"yyyyMMddHH") ;
                String faceErrKey ="FaceErr:" + companyID+":" + machno + ":" + date;
                if(ids.length>0 && list.size()>0){
                    for (String pId : list){
                        if(CommonUtil.isNull(pId)){
                            continue;
                        }
                        idsLong.add(Long.valueOf(pId));

                        redisUtil.hmSet(faceErrKey,pId,pId);
                    }
                }
                redisUtil.expire(faceErrKey,86400l);
            }catch (Exception e){
                log.error("faceLogin exception :【{}】,Url:faceLogin" ,e);
            }

            FaceErrVo faceErrVo =new FaceErrVo();
            faceErrVo.setComId(companyID.longValue());
            faceErrVo.setDeviceNo(machno);
            faceErrVo.setDeviceType(1);
            faceErrVo.setTimestamp(System.currentTimeMillis());
            faceErrVo.setLoginNum(loginNum);
            faceErrVo.setIds(idsLong);

            log.info("机器异常人脸 【{}】【{}】",machno,faceErrVo);

        }catch (Exception e){
            log.error("机器号:【{}】,Url:faceLogin,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
        return JsonResponse.success();
    }

    @Override
    public JsonResponse adPic(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Integer companyID= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"companyId");
            Result result = payConfigClientService.adList(companyID.longValue(),1,1);

            return JsonResponse.success(result.getData());
        }catch (Exception e){
            log.error("机器号:【{}】,Url:faceLogin,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse pictureurl(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");

            String tradeNo = (String) map.get("tradeNo");
            String url = (String) map.get("url");
            String tradeTime = (String) map.get("tradeTime");

            Date orderTime = DateUtils.strToDate(tradeTime);
            //商户编号
            Integer companyID= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"companyId");

            EntityXfRecord entityXfRecord = new EntityXfRecord();
            entityXfRecord.setComId(companyID.longValue());
            entityXfRecord.setOrderNo(tradeNo);
            entityXfRecord.setDeviceNo(machno);
            entityXfRecord.setMonthValue(DateUtils.getStrDate(orderTime,"yyyyMM"));

            entityXfRecord.setFace(url);

            xfRecodeClientService.updateXFace(entityXfRecord);

            try{
                payoperFeignService.updateXfFace(entityXfRecord);
            }catch (Exception e){
            }
            return JsonResponse.success();
        }catch (Exception e){


            log.error("机器号:【{}】,Url:faceLogin,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse passwordAuthentication(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");
            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            Long pId= Long.parseLong(String.valueOf(map.get("P_ID")));
            String password= String.valueOf(map.get("password"));
            //平台编号为1按照本平台走
            if (saasNo==1){
                Result result=payoperFeignService.verifyPassword(companyID,pId,password);
                if (result.getCode()==200){
                    return JsonResponse.success(result.getData());
                }else {
                    return JsonResponse.error(String.valueOf(result.getCode()),result.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e.getMessage());
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse menuList(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");

            Integer listLimit= 10;
            Object  limitObj  = map.get("listLimit");
            if(limitObj!=null){
                listLimit= (Integer) limitObj;
            }

            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                Result result=payoperFeignService.getDishesWhiteList(companyID,machno,listLimit);
                if (result.getCode()==200){
                    return JsonResponse.success(result.getData());
                }else {
                    return JsonResponse.error(String.valueOf(result.getCode()),result.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse menuListResult(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");

            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                Result result=payoperFeignService.changeDishesWhiteState(companyID,machno);
                if (result.getCode()==200){
                    return JsonResponse.success(result.getData());
                }else {
                    return JsonResponse.error(String.valueOf(result.getCode()),result.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse RegisterInf(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");

            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                RegisterPersonRequest request =new RegisterPersonRequest();
                request.setMachNo(machno);
                request.setCompanyId(companyID);
                request.setUrl((String) map.get("url"));
                request.setPhone((String) map.get("phone"));
                Result result =  payoperFeignService.registerInf(request);
                if (result.getCode()==200){
                    return JsonResponse.success(result.getData());
                }else {
                    return JsonResponse.error(String.valueOf(result.getCode()),result.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse SendSMS(Map<String, Object> map) {
        String machno="";
        try {
            //机器号
            machno= (String) map.get("machNo");

            //商户编号
            Long companyID= Long.parseLong(String.valueOf(map.get("companyID")));
            Integer saasNo= (Integer) redisUtil.hmGet("HttpXfDevice:"+machno,"saasNo");
            //平台编号为1按照本平台走
            if (saasNo==1){
                InvitedSmsRequest request =new InvitedSmsRequest();
                request.setComId(companyID);
                request.setPhone((String) map.get("phone"));
                request.setFlag((Integer) map.get("flag"));
                request.setPId(Long.valueOf((String) map.get("pid")));
                request.setTradeNo((String) map.get("tradeNo"));
                request.setTradeTime((String) map.get("tradeTime"));
                Result result =  payoperFeignService.sendSMS(request);
                if (result.getCode()==200){
                    return JsonResponse.success(result.getData());
                }else {
                    return JsonResponse.error(String.valueOf(result.getCode()),result.getMsg());
                }
            }else {
                return JsonResponse.error(StateCode.saasNoCod,StateCode.saasNoMsg);
            }
        }catch (Exception e){
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】",machno,map,e);
            return JsonResponse.error();
        }
    }

}
